//
//  ESTFollowUpTableViewCell.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/18.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTFollowUpTableViewCell.h"

@interface ESTFollowUpTableViewCell ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *leftTitleLabel;
@property (weak, nonatomic) IBOutlet UITextField *rightTextField;
@property (weak, nonatomic) IBOutlet UIView *rightView;
@property (weak, nonatomic) IBOutlet UIView *selectedView;
@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
@property (weak, nonatomic) IBOutlet UITextField *selectedTextLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftTitleMargin;
/**
 *  存储选择项的按钮
 */
@property(nonatomic, strong) NSArray<TYZButton *> *buttonArray;
/**
 *  到场人数的textField
 */
@property(nonatomic, strong) TYZTextField *textField;
@end

@implementation ESTFollowUpTableViewCell

+ (instancetype)creatFollowUpTableViewCell:(UITableView *)tableView {
    ESTFollowUpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTFollowUpTableViewCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ESTFollowUpTableViewCell" owner:nil options:nil].lastObject;
    }
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.mySwitch.onTintColor = ThemeColor;
    self.rightTextField.delegate = self;
}
- (void)setupSelectedOption {
    if (self.optionArray.count < 2) return;
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:self.optionArray.count];
    CGFloat width = self.optionArray.firstObject.length * 15 + 40;
    CGFloat height = 30;
    [self.optionArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        TYZButton *button = [[TYZButton alloc] init];
        [self.rightView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.rightView);
            make.left.equalTo(self.rightView).offset((width + 10) * idx);
            make.width.mas_equalTo(width);
            make.height.mas_equalTo(height);
        }];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = height * 0.5;
        button.layer.borderColor = TYZLightMMColor.CGColor;
        button.titleLabel.font = self.leftTitleLabel.font;
        [button setTitle:obj forState:UIControlStateNormal];
        button.tag = TAG_SIXTH_ACTIVITY_SELECTOPTIONTags + idx;
        [self setupOptionWithButton:button selected:idx == self.optionInteger];
        [button addTarget:self action:@selector(didClickSelectedOptionButton:) forControlEvents:UIControlEventTouchUpInside];
        [arrM addObject:button];
    }];
    self.buttonArray = [arrM mutableCopy];
}
- (void)setupControllValueChanged {
    UIFont *font = [UIFont systemFontOfSize:CoreInfo.fontSize * 1.2];
    CGFloat width = 20;
    TYZButton *leftButton = [[TYZButton alloc] init];
    [self.rightView addSubview:leftButton];
    [leftButton setTitle:@"-" forState:UIControlStateNormal];
    [leftButton setTitleColor:TYZLightColor forState:UIControlStateNormal];
    leftButton.layer.borderColor = TYZLightColor.CGColor;
    leftButton.layer.borderWidth = 1;
    leftButton.titleLabel.font = font;
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.leftTitleLabel);
        make.left.equalTo(self.rightView);
        make.width.height.mas_equalTo(width);
    }];
    TYZTextField *textField = [[TYZTextField alloc] init];
    [self.rightView addSubview:textField];
    textField.text = [NSString stringWithFormat:@"%ld", (long)self.controlValue];
    textField.font = font;
    textField.textColor = TYZBlackColor;
    textField.textAlignment = NSTextAlignmentCenter;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftButton);
        make.left.equalTo(leftButton.mas_right);
        make.width.mas_equalTo(width * 2);
        make.height.mas_equalTo(width);
    }];
    textField.delegate = self;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.returnKeyType = UIReturnKeyDone;
    textField.borderStyle = UITextBorderStyleNone;
    textField.tag = TAG_SIXTH_ACTIVITY_FOLLOWUP_PERSON;
    self.textField = textField;
    TYZButton *rightButton = [[TYZButton alloc] init];
    [self.rightView addSubview:rightButton];
    [rightButton setTitle:@"+" forState:UIControlStateNormal];
    [rightButton setTitleColor:TYZLightColor forState:UIControlStateNormal];
    rightButton.layer.borderColor = TYZLightColor.CGColor;
    rightButton.layer.borderWidth = 1;
    rightButton.titleLabel.font = font;
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftButton);
        make.left.equalTo(textField.mas_right);
        make.width.height.mas_equalTo(width);
    }];
    [leftButton addTarget:self action:@selector(didClickLeftButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton addTarget:self action:@selector(didClickRightButton) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSInteger maxWords = self.maxWords;
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if (maxWords == 0) {
        maxWords = FollowUpTableViewCellTextFieldMaxWords;
    }
    if (textField.text.length + string.length > maxWords) {
        return NO;
    }
    if (textField.tag == TAG_SIXTH_ACTIVITY_FOLLOWUP_PERSON) {
        if ([string isValidateRegex:Regex_Number]) {
            if ([textField.text integerValue] > self.maxValue) {
                return NO;
            }
            return YES;
        }
            return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    if (self.didClickKeyboardReturnBlock) {
        self.didClickKeyboardReturnBlock();
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField.tag == TAG_SIXTH_ACTIVITY_FOLLOWUP_PERSON) {
        NSInteger integer = [textField.text integerValue];
        if (integer > self.maxValue) {
            integer = self.maxValue;
        }
        if (integer < 1) {
            integer = 1;
        }
        self.controlValue = integer;
        textField.text = [NSString stringWithFormat:@"%ld", (long)integer];
    }
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {

}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    //获取当前cell相对屏幕的位置
    CGRect frame = [textView convertRect:textView.frame toView:nil];
    if (self.cellBecomeFirstResponderBlock) {
        self.cellBecomeFirstResponderBlock(frame);
    }
    return YES;
}
#pragma mark - actions
- (void)didClickSelectedOptionButton:(TYZButton *)button {
    [self.buttonArray enumerateObjectsUsingBlock:^(TYZButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self setupOptionWithButton:obj selected:idx == button.tag - TAG_SIXTH_ACTIVITY_SELECTOPTIONTags];
    }];
    if (self.selectedOptionBlock) {
        self.selectedOptionBlock(button.tag - TAG_SIXTH_ACTIVITY_SELECTOPTIONTags);
    }
}
- (void)setupOptionWithButton:(TYZButton *)button selected:(BOOL)selected {
    if (selected) {
        [button setTitleColor:TYZWhiteColor forState:UIControlStateNormal];
        [button setBackgroundColor:ThemeColor];
        button.layer.borderWidth = 0.0;
    } else {
        [button setTitleColor:TYZLightColor forState:UIControlStateNormal];
        [button setBackgroundColor:TYZClearColor];
        button.layer.borderWidth = 1.0;
    }
}
- (void)didClickLeftButton {
    self.controlValue--;
    if (self.controlValue < 1) {
        self.controlValue = 1;
    }
    self.textField.text = [NSString stringWithFormat:@"%ld", (long)self.controlValue];
    if (self.controlValueChangedBlock) {
        self.controlValueChangedBlock(self.controlValue);
    }
}
- (void)didClickRightButton {
    self.controlValue++;
    if (self.controlValue > self.maxValue) {
        self.controlValue = self.maxValue;
    }
    self.textField.text = [NSString stringWithFormat:@"%ld", (long)self.controlValue];
    if (self.controlValueChangedBlock) {
        self.controlValueChangedBlock(self.controlValue);
    }
}
- (IBAction)didSelectedCoverButton:(UIButton *)sender {
    if (self.didSelectedCoverBlock) {
        self.didSelectedCoverBlock();
    }
}
- (IBAction)didChangedSwitch:(UISwitch *)sender {
    if (self.didSelectedSwitchBlock) {
        self.didSelectedSwitchBlock(sender.on);
    }
}
#pragma mark - stter & getter
- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    self.cellType = [dataArray.firstObject integerValue];
    self.leftTitle = dataArray[1];
}
- (void)setCellType:(ESTFollowUpTableViewCellType)cellType {
    _cellType = cellType;
    switch (cellType) {
        case FollowUpTableViewCellType_TextField:
        {
            self.mySwitch.hidden = YES;
            self.rightTextField.hidden = NO;
            self.selectedView.hidden = YES;
            self.leftTitleLabel.textColor = TYZLightColor;
            self.rightTextField.placeholder = self.dataArray[2];
            self.keyboardType = [self.dataArray[3] integerValue];
            self.rightTextField.tag = [self.dataArray[4] integerValue];
            if ([self.dataArray[5] integerValue]) {
                [self.rightTextField becomeFirstResponder];
            }
            if ([self.dataArray[7] integerValue] == 0) {
                self.leftTitleMargin.constant = 30;
                self.leftTitleLabel.textColor = TYZLightMColor;
            } else {
                self.leftTitleMargin.constant = 20;
            }
            self.rightTextField.returnKeyType = [self.dataArray[6] integerValue];
            NSString *str = self.dataArray[8];
            if (self.keyboardType == UIKeyboardTypeNumberPad) {
                if ([str integerValue] == 0) {
                    str = @"";
                }
            }
            self.rightTextField.text = str;
            if ([self.dataArray[1] isEqualToString:[@"到场客户姓名" S]]) {
                self.rightTextField.enabled = NO;
            }
        }
            break;
        case FollowUpTableViewCellType_Options:
            self.mySwitch.hidden = YES;
            self.rightTextField.hidden = YES;
            self.selectedView.hidden = YES;
            self.leftTitleMargin.constant = 20;
            self.leftTitleLabel.textColor = TYZLightColor;
            self.optionArray = self.dataArray[2];
            self.optionInteger = [self.dataArray[3] integerValue];
            break;
        case FollowUpTableViewCellType_SelectedBlock:
            self.rightTextField.hidden = YES;
            self.mySwitch.hidden = YES;
            self.selectedView.hidden = NO;
            self.leftTitleLabel.textColor = TYZLightColor;
            self.selectedPlaceholder = self.dataArray[2];
            if ([self.dataArray[3] integerValue] == 0) {
                self.leftTitleMargin.constant = 30;
                self.leftTitleLabel.textColor = TYZLightMColor;
            } else {
                self.leftTitleMargin.constant = 20;
            }
            self.selectedTextLabel.text = self.dataArray[4];
            break;
        case FollowUpTableViewCellType_Switch:
            self.mySwitch.hidden = NO;
            self.rightTextField.hidden = YES;
            self.selectedView.hidden = YES;
            self.leftTitleMargin.constant = 20;
            self.leftTitleLabel.textColor = TYZLightColor;
            self.switchOpened = [self.dataArray[2] integerValue];
            break;
        case FollowUpTableViewCellType_Number:
            self.mySwitch.hidden = YES;
            self.rightTextField.hidden = YES;
            self.selectedView.hidden = YES;
            self.leftTitleMargin.constant = 30;
            self.leftTitleLabel.textColor = TYZLightColor;
            self.controlValue = [self.dataArray[2] integerValue];
            self.maxValue = [self.dataArray[3] integerValue];
            break;
        default:
            break;
    }
}
- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    self.leftTitleLabel.text = leftTitle;
}
- (void)setRightPlaceholder:(NSString *)rightPlaceholder {
    _rightPlaceholder = rightPlaceholder;
    self.rightTextField.placeholder = rightPlaceholder;
}
- (void)setSelectedPlaceholder:(NSString *)selectedPlaceholder {
    _selectedPlaceholder = selectedPlaceholder;
    self.selectedTextLabel.placeholder = selectedPlaceholder;
}
- (void)setSwitchOpened:(BOOL)switchOpened {
    _switchOpened = switchOpened;
    self.mySwitch.on = switchOpened;
}
- (void)setKeyboardType:(UIKeyboardType)keyboardType {
    _keyboardType = keyboardType;
    self.rightTextField.keyboardType = keyboardType;
}
- (void)setOptionArray:(NSArray<NSString *> *)optionArray {
    _optionArray = optionArray;
}
- (void)setOptionInteger:(NSInteger)optionInteger {
    _optionInteger = optionInteger;
    [self setupSelectedOption];
}
- (void)setMaxValue:(NSInteger)maxValue {
    _maxValue = maxValue;
    [self setupControllValueChanged];
}
@end
